package org.misalen.web.admin.controllers;

import java.util.Date;

import org.misalen.web.admin.util.ServerResult;
import org.misalen.web.database.model.mysql.pro.ProUserExtractCash;
import org.misalen.web.database.repository.mysql.ProUserExtractCashRepository;
import org.misalen.web.database.util.PaginationFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/pro-user-extract")
public class ProUserExtractCashController extends BaseController {

	@Autowired
	protected ProUserExtractCashRepository proUserExtractCashRepository;
	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/apply/", method = { RequestMethod.GET })
	public ModelAndView list1() {
		ModelAndView modelAndView = new ModelAndView("/proUserExtractCash/apply/list");
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/apply/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost(PaginationFrom<ProUserExtractCash> pagination) {
		PaginationFrom<ProUserExtractCash> from = proUserExtractCashRepository
				.findPageByApply("not-operating", getShiroUser().getBelongCity(), pagination);
		return renderSuccess(from);
	}

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/cash/", method = { RequestMethod.GET })
	public ModelAndView list2() {
		ModelAndView modelAndView = new ModelAndView("/proUserExtractCash/cash/list");
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/cash/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost2(PaginationFrom<ProUserExtractCash> pagination, String state) {
		if (state == null || state.equals("")) {
			state = "complete";
		}
		ProUserExtractCash proUserExtractCash = new ProUserExtractCash();
		proUserExtractCash.setState(state);
		PaginationFrom<ProUserExtractCash> from = proUserExtractCashRepository.findPageByApply(state, getShiroUser().getBelongCity(),
				pagination);
		return renderSuccess(from);
	}

	@RequestMapping(value = "/apply/complete/{primaryKey}", method = { RequestMethod.GET })
	public @ResponseBody ServerResult complete(@PathVariable String primaryKey) {
		ProUserExtractCash userExtractCash = proUserExtractCashRepository
				.get(ProUserExtractCash.class, primaryKey);
		userExtractCash.setState("complete");
		userExtractCash.setProcessingTime(new Date());
		proUserExtractCashRepository.update(userExtractCash);
		proUserRepository.updateWithDraw(userExtractCash.getAmount().intValue(), userExtractCash.getUserId());
		
		return ServerResult.ok();
	}

	@RequestMapping(value = "/apply/refuse/{primaryKey}", method = { RequestMethod.GET })
	public @ResponseBody ServerResult refuse(@PathVariable String primaryKey) {
		ProUserExtractCash userExtractCash = proUserExtractCashRepository
				.get(ProUserExtractCash.class, primaryKey);
		userExtractCash.setState("refuse");
		userExtractCash.setProcessingTime(new Date());
		proUserExtractCashRepository.update(userExtractCash);
		proUserRepository.updateBalance(userExtractCash.getAmount().intValue(), userExtractCash.getUserId());
		return ServerResult.ok();
	}
}
